www.gusucode.com > VC++ 对话框中使用打印预览的功能演示-源码程序 > VC++ 对话框中使用打印预览的功能演示-源码程序/code/DlgPreprintDlg.cpp
//Download by http://www.NewXing.com // DlgPreprintDlg.cpp : implementation file // #include "stdafx.h" #include "DlgPreprint.h" #include "DlgPreprintDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif void Drawf(CDC* pDC,CPrintInfo* pInfo,void* pVoid=NULL); ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDlgPreprintDlg dialog CDlgPreprintDlg::CDlgPreprintDlg(CWnd* pParent /*=NULL*/) : CDialog(CDlgPreprintDlg::IDD, pParent) { //{{AFX_DATA_INIT(CDlgPreprintDlg) m_FontSize = 12; m_FontName = -1; m_Str = _T(" 本程序主要演示如何在对话框中使用只有在文档-视图模型中才能使用的打印预览功能,其实本程序也使用了一个没有文档类的文档-视图结构来间接完成打印预工作的,但我们在使用时基本感觉不到它的存在。\r\n 在实际使用中可将文档-视图部分编制成扩展动态链接库,我就是这样做的,编制打印程序时使用起来非常方便。"); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CDlgPreprintDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDlgPreprintDlg) DDX_Control(pDX, IDC_COMBO1, m_FontList); DDX_Text(pDX, IDC_EDIT_FONTSIZE, m_FontSize); DDX_CBIndex(pDX, IDC_COMBO1, m_FontName); DDX_Text(pDX, IDC_EDIT1, m_Str); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDlgPreprintDlg, CDialog) //{{AFX_MSG_MAP(CDlgPreprintDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON1, OnButton1) ON_BN_CLICKED(IDC_BUTTON2, OnButton2) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDlgPreprintDlg message handlers BOOL CDlgPreprintDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, _T("关于本程序")); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon EnumerateFonts(); m_FontList.SelectString(0,_T("宋体")); // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control } BOOL CALLBACK AFX_EXPORT CDlgPreprintDlg::EnumFamScreenCallBackEx(ENUMLOGFONTEX* pelf, NEWTEXTMETRICEX* /*lpntm*/, int FontType, LPVOID pThis) { // don't put in non-printer raster fonts if (FontType & RASTER_FONTTYPE) return 1; CString ss; if(FontType & TRUETYPE_FONTTYPE) ss=_T(pelf->elfLogFont.lfFaceName); int kk=((CDlgPreprintDlg*)pThis)->m_FontList.AddString(ss); //if(ss==_T(logFont) ((CTextProperties*)pThis)->m_FontName.SetCurSel(kk); return 1; // Call me back } BOOL CDlgPreprintDlg::EnumerateFonts() { HDC hDC; // Get screen fonts hDC = ::GetWindowDC(NULL); LOGFONT lf; ZeroMemory(&lf,sizeof(lf)); lf.lfCharSet = GB2312_CHARSET; if (!EnumFontFamiliesEx( hDC, // handle to device context &lf, // pointer to logical font information (FONTENUMPROC)EnumFamScreenCallBackEx, // pointer to callback function (LPARAM) this, // application-supplied data (DWORD) 0)) return FALSE; ::ReleaseDC(NULL,hDC); return TRUE; // All's ok } void CDlgPreprintDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CDlgPreprintDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CDlgPreprintDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CDlgPreprintDlg::OnButton1() { // TODO: Add your control notification handler code here //初始化文档-视图结构时需传递五个参数 //DRAWFUN pDraw 显示内容函数指针 //CWnd*pOldWnd 主对话框窗口指针 //CWnd*pCallWnd 调用窗口指针,在一个对话杠程序中,pOldWnd和pCallWnd是一样的 //若不是在主对话框中调用,则pOldWnd为主对话框指针,而pCallWnd为调用窗口指针 //BOOL bDirect 是否直接打印 //LPCTSTR stTitle 预览窗口标题 m_PreFrame = new CMainFrame(Drawf,this,this,FALSE); m_PreFrame->ShowWindow(SW_SHOW); m_PreFrame->UpdateWindow(); } void CDlgPreprintDlg::OnButton2() { m_PreFrame = new CMainFrame(Drawf,this,this,TRUE); } void CDlgPreprintDlg::OnPrint(CDC* pDC,CPrintInfo* pInfo) { //UpdateData(TRUE); CFont font,*oldfont; CPoint point; CString ss=_T("在对话框中使用打印预览"); int nWidth=pDC->GetDeviceCaps(HORZRES); int nHeight=pDC->GetDeviceCaps(VERTRES); int ghdf=pDC->GetDeviceCaps(LOGPIXELSY); CRect DrawRect(0,0,nWidth,nHeight),rect,tempRect; DrawRect.InflateRect(-100,-50,-100,-20); pDC->Rectangle(&DrawRect); font.CreateFont(-MulDiv(32,-pDC->GetDeviceCaps(LOGPIXELSY),72), 0,0,0,FW_NORMAL,0,0,0,GB2312_CHARSET, OUT_STROKE_PRECIS,CLIP_STROKE_PRECIS,DRAFT_QUALITY, VARIABLE_PITCH|FF_SWISS,_T("黑体")); oldfont=pDC->SelectObject(&font); tempRect=DrawRect; tempRect.InflateRect(0,-5,0,0); pDC->DrawText(ss,&tempRect,DT_CALCRECT|DT_SINGLELINE); rect=tempRect;rect.InflateRect(10,0); point=CPoint(DrawRect.left+(DrawRect.Width()-rect.Width())/2,rect.bottom+2); pDC->MoveTo(point); pDC->LineTo(point.x+rect.Width(),point.y); point.Offset(0,2); pDC->MoveTo(point); pDC->LineTo(point.x+rect.Width(),point.y); tempRect.right=DrawRect.right; pDC->DrawText(ss,&tempRect,DT_CENTER|DT_TOP|DT_SINGLELINE); pDC->SelectObject(oldfont); font.DeleteObject(); font.CreateFont(-MulDiv(12,-pDC->GetDeviceCaps(LOGPIXELSY),72), 0,0,0,FW_NORMAL,0,0,0,GB2312_CHARSET, OUT_STROKE_PRECIS,CLIP_STROKE_PRECIS,DRAFT_QUALITY, VARIABLE_PITCH|FF_SWISS,_T("宋体")); pDC->SelectObject(&font); ss=_T("HRX hrxzh@163.net"); rect=DrawRect; rect.top=point.y+5; pDC->DrawText(ss,&rect,DT_CALCRECT|DT_SINGLELINE); rect.right=DrawRect.right; pDC->DrawText(ss,&rect,DT_CENTER|DT_TOP|DT_SINGLELINE); pDC->SelectObject(oldfont); font.DeleteObject(); //以下打印定制内容 UpdateData(TRUE); if(m_Str.IsEmpty()) return; CString stFontName; if(m_FontName>-1) m_FontList.GetLBText(m_FontName, stFontName); else stFontName=_T("宋体"); if(stFontName.IsEmpty()) stFontName=_T("宋体"); if(m_FontSize==0) m_FontSize=9; font.CreateFont(-MulDiv(m_FontSize,-pDC->GetDeviceCaps(LOGPIXELSY),72), 0,0,0,FW_NORMAL,0,0,0,GB2312_CHARSET, OUT_STROKE_PRECIS,CLIP_STROKE_PRECIS,DRAFT_QUALITY, VARIABLE_PITCH|FF_SWISS,stFontName); tempRect.top=rect.bottom+10; tempRect.left=DrawRect.left+30; tempRect.right=DrawRect.right-30; tempRect.bottom=DrawRect.bottom-50; pDC->SelectObject(&font); pDC->DrawText(m_Str,&tempRect,DT_TOP|DT_WORDBREAK); pDC->SelectObject(oldfont); font.DeleteObject(); } void Drawf(CDC* pDC,CPrintInfo* pInfo,void* pVoid) { ((CDlgPreprintDlg*)pVoid)->OnPrint(pDC,pInfo); }